<template>
  <div id="table_container">
    <el-card style="margin-bottom: 5px; text-align: left">
      <el-input v-model="keyWord" placeholder="输入搜索内容" style="width: 200px;" @keyup.enter.native="handleFilter"></el-input>
      <el-select v-model="status" placeholder="状态" style="width: 90px;margin-left: 10px;">
        <el-option v-for="item in importanceOptions" :key="item.value" :label="item.label" :value="item.value"/>
      </el-select>
      <el-button type="primary" icon="el-icon-search" @click="handleFilter()" style="margin-left: 10px;">
        查询
      </el-button>
      <el-button class="create_btn" style="float: right;" type="primary" icon="el-icon-edit" @click="handleCreate">
        新增
      </el-button>
    </el-card>
    <el-card>
      <el-table
          :data="tableData"
          v-loading="loading"
          row-key="id"
          border>
        <el-table-column
            type="index"
            label="序号"
            align="center"
            width="50">
        </el-table-column>
        <el-table-column
            prop="label"
            label="字典名称">
        </el-table-column>
        <el-table-column
            prop="value"
            label="字典编码">
        </el-table-column>
        <el-table-column
            prop="status"
            label="状态"
            align="center"
            width="70">
          <template slot-scope="scope">
            <el-tag
                :type="scope.row.status === true ? 'success' :'danger' "
                disable-transitions>{{ scope.row.status_label }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
            prop="sort"
            label="排序"
            align="center"
            width="50">
        </el-table-column>
        <el-table-column
            prop="description"
            label="备注"
            align="center">
        </el-table-column>
        <el-table-column
            label="更新时间"
            align="center"
            width="120">
          <template slot-scope="scope">
            <i class="el-icon-time"></i>
            <span style="margin-left: 10px">{{ scope.row.update_time }}</span>
          </template>
        </el-table-column>
        <el-table-column
            label="创建时间"
            align="center"
            width="120">
          <template slot-scope="scope">
            <i class="el-icon-time"></i>
            <span style="margin-left: 10px">{{ scope.row.create_time }}</span>
          </template>
        </el-table-column>

        <el-table-column
            fixed="right"
            width="300"
            align="center"
            label="操作">
          <template slot-scope="scope">
            <!--            <el-button @click="handleDetail(scope.row)" title="查看详情" icon="el-icon-search" circle></el-button>-->
            <el-button v-permission="['dict:edit']" @click="handleEdit(scope.row)" type="primary" icon="el-icon-edit">编辑</el-button>
            <el-button v-permission="['dict:delete']" @click="handleDelete(scope.row)" type="danger" icon="el-icon-delete">删除</el-button>
            <el-button v-permission="['dict:value']" @click="handleChange(scope.row)" type="warning" icon="el-icon-s-operation" v-if="!scope.row.is_catalog">配置属性
            </el-button>
          </template>
        </el-table-column>

      </el-table>
    </el-card>

    <el-dialog title="新建" width="400px"  @close="handleClose" :visible.sync="createDialogFormVisible">
      <el-form :model="form" style="text-align: left">
        <el-form-item label="字典名称" :label-width="formLabelWidth">
          <el-input v-model="form.label" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="字典编码" :label-width="formLabelWidth">
          <el-input v-model="form.value" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="排序" :label-width="formLabelWidth">
          <el-input-number v-model="form.sort"></el-input-number>
        </el-form-item>

        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-radio-group v-model="form.status">
            <el-radio :label="true">启用</el-radio>
            <el-radio :label="false">停用</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="备注" :label-width="formLabelWidth">
          <el-input
              type="textarea"
              :autosize="{ minRows: 4, maxRows: 8}"
              placeholder="请输入内容"
              v-model="form.description">
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="createDialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitCreate">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="编辑" width="400px"  @close="handleClose" :visible.sync="editDialogFormVisible" >
      <el-form :model="form" style="text-align: left">
        <el-form-item label="字典名称" :label-width="formLabelWidth">
          <el-input v-model="form.label" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="字典编码" :label-width="formLabelWidth">
          <el-input v-model="form.value" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="排序" :label-width="formLabelWidth">
          <el-input-number v-model="form.sort"></el-input-number>
        </el-form-item>

        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-radio-group v-model="form.status">
            <el-radio :label="true">启用</el-radio>
            <el-radio :label="false">停用</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="备注" :label-width="formLabelWidth">
          <el-input
              type="textarea"
              :autosize="{ minRows: 4, maxRows: 8}"
              placeholder="请输入内容"
              v-model="form.description">
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editDialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitUpdate">确 定</el-button>
      </div>
    </el-dialog>

    <el-drawer
        title="字典列表"
        :visible.sync="drawer"
        size="40%"
        :before-close="handleCloseDrawer">
      <el-button style="float: left; margin-left: 20px;" size="mini" type="primary" icon="el-icon-edit" @click="handleCreateValue">
        新增
      </el-button>
      <el-table
          v-loading="valueTableLoading"
          :data="valueTableData"
          size="medium"
          style="width: 100%;margin-top: 30px;">
        <el-table-column
            prop="label"
            label="名称">
        </el-table-column>
        <el-table-column
            prop="value"
            label="数据值">
        </el-table-column>
        <el-table-column
            prop="status"
            label="状态"
            align="center"
            width="70">
          <template slot-scope="scope">
            <el-tag
                :type="scope.row.status === true ? 'success' :'danger' "
                disable-transitions>{{ scope.row.status_label }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
            fixed="right"
            width="180"
            align="center"
            label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEditValue(scope.row)" title="编辑" type="primary" icon="el-icon-edit"></el-button>
            <el-button size="mini" @click="handleDelete(scope.row)" title="删除" type="danger" icon="el-icon-delete"></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-drawer>
    <el-dialog title="新建"  top="10vh" width="400px" @close="handleClose" :visible.sync="ValuecreateDialogFormVisible">
      <el-form :model="form" style="text-align: left">
        <el-form-item label="字典名称" :label-width="formLabelWidth">
          <el-input v-model="form.label" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="数据值类型" :label-width="formLabelWidth">
          <el-select v-model="form.type" style="width: 100%">
            <el-option v-for="item in typeOptions" :key="item.value" :label="item.label" :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="数据值" :label-width="formLabelWidth">
          <el-input v-model="form.value" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="排序" :label-width="formLabelWidth">
          <el-input-number v-model="form.sort"></el-input-number>
        </el-form-item>

        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-radio-group v-model="form.status">
            <el-radio :label="true">启用</el-radio>
            <el-radio :label="false">停用</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="备注" :label-width="formLabelWidth">
          <el-input
              type="textarea"
              :autosize="{ minRows: 4, maxRows: 8}"
              placeholder="请输入内容"
              v-model="form.description">
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="ValuecreateDialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitCreateValue">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="编辑"  top="10vh" width="400px"  @close="handleClose" :visible.sync="ValueeditDialogFormVisible" >
      <el-form :model="form" style="text-align: left">
        <el-form-item label="字典名称" :label-width="formLabelWidth">
          <el-input v-model="form.label" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="数据值类型" :label-width="formLabelWidth">
          <el-select v-model="form.type" style="width: 100%">
            <el-option v-for="item in typeOptions" :key="item.value" :label="item.label" :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="数据值" :label-width="formLabelWidth">
          <el-input v-model="form.value" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="排序" :label-width="formLabelWidth">
          <el-input-number v-model="form.sort"></el-input-number>
        </el-form-item>

        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-radio-group v-model="form.status">
            <el-radio :label="true">启用</el-radio>
            <el-radio :label="false">停用</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="备注" :label-width="formLabelWidth">
          <el-input
              type="textarea"
              :autosize="{ minRows: 4, maxRows: 8}"
              placeholder="请输入内容"
              v-model="form.description">
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="ValueeditDialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitUpdateValue">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {createDictionary, deleteDictionary, getDictionary, updateDictionary} from "@/api";

export default {
  name: "index",
  data() {
    return {
      loading: true,
      tableData: [],
      createDialogFormVisible: false,
      showDialogFormVisible: false,
      editDialogFormVisible: false,
      ValuecreateDialogFormVisible: false,
      ValueshowDialogFormVisible: false,
      ValueeditDialogFormVisible: false,
      form: {
        name: undefined,
        key: undefined,
        sort: 1,
        description: undefined,
        status: true,
      },
      formLabelWidth: '90px',
      keyWord: undefined,
      importanceOptions: [{label: '启用', value: 1}, {label: '停用', value: 0}],
      status: undefined,

      menuOptions: [],
      drawer: false,

      valueTableData: [],
      typeOptions: [
        {label: 'text', value: 0},
        {label: 'number', value: 1},
        {label: 'date', value: 2},
        {label: 'datetime', value: 3},
        {label: 'time', value: 4},
        {label: 'boolean', value: 5},
      ],
      valueTableLoading: true
    }
  },
  mounted() {
    this.getDataList()
  },
  methods: {
    getDataList() {
      getDictionary().then(res => {
        this.tableData = res.data
        setTimeout(() => {
          this.loading = false
        }, 1.5 * 500);
      })
    },
    handleClose() {
      this.form = {
        name: undefined,
        key: undefined,
        sort: 1,
        description: undefined,
        status: true,
      }
    },
    handleClick(row) {
      console.log(row);
    },
    handleFilter() {
      this.getDataList()
    },
    handleCreate() {
      this.createDialogFormVisible = true
      // this.loadDialogOptions()
    },
    handleDetail(row) {
      this.showDialogFormVisible = true
      this.form = {...row}
    },
    handleEdit(row) {
      this.editDialogFormVisible = true
      this.form = {...row}
    },
    handleDelete(row) {
      deleteDictionary(row.id).then(res => {
        this.$message.success('删除成功！');
        this.$router.go(0)
      })

    },
    submitCreate() {
      let data = this.form
      createDictionary(data).then(res => {
        this.$message.success('新建成功！')
        this.createDialogFormVisible = false
        this.$router.go(0)
      })
    },
    submitUpdate() {
      let data = this.form
      updateDictionary(data).then(res => {
        this.$message.success('修改成功！')
        this.editDialogFormVisible = false
        this.$router.go(0)
      })
    },
    getValueDataList(params) {
      this.drawer = true
      getDictionary(params).then(res => {
        this.valueTableData = res.data

        setTimeout(() => {
          this.valueTableLoading = false
        }, 1.5 * 500);
      })
    },
    handleChange(row) {
      this.getValueDataList({parentId: row.id})
      this.form.parent = row.id
    },
    handleCloseDrawer(done) {
      this.form = {
        name: undefined,
        key: undefined,
        sort: 1,
        description: undefined,
        status: true,
      }
      done();
    },
    handleCreateValue() {
      this.ValuecreateDialogFormVisible = true
      // this.loadDialogOptions()
      this.form.is_value = true
    },
    handleDetailValue(row) {
      this.ValueshowDialogFormVisible = true
      this.form = {...row}
    },
    handleEditValue(row) {
      this.ValueeditDialogFormVisible = true
      this.form = {...row}
    },
    submitCreateValue() {
      let data = this.form
      createDictionary(data).then(res => {
        this.$message.success('新建成功！')
        this.ValuecreateDialogFormVisible = false
        this.getValueDataList({parentId: data.parent})
      })
    },
    submitUpdateValue() {
      let data = this.form
      updateDictionary(data).then(res => {
        this.$message.success('修改成功！')
        this.ValueeditDialogFormVisible = false
        this.getValueDataList({parentId: this.form.parent})
      })
    },


  },
}
</script>

<style scoped>

</style>